package umn.angrypenguins.monopoly;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import umn.angrypenguins.monopoly.models.PlayerInstance;
import umn.angrypenguins.monopoly.webservice.WebServiceClient;

public class JoinActivity extends Activity implements View.OnClickListener{

    class JoinRequestWrapper {
        public String gameName;
        public String playerName;
    }

    class JoinGameTask extends AsyncTask<JoinRequestWrapper, PlayerInstance, PlayerInstance> {
        @Override
        protected PlayerInstance doInBackground(JoinRequestWrapper... wrappers) {
            return WebServiceClient.joinGame(wrappers[0].gameName, wrappers[0].playerName);
        }

        protected void onPostExecute(PlayerInstance createdPlayer) {
            super.onPostExecute(createdPlayer);
            //newContact = contact2;

            if( createdPlayer == null )
                Log.i("JoinActivity.JoinGameTask.onPostExecute", "createdPlayer is NULL");

            Log.i("JoinActivity.JoinGameTask.onPostExecute", "createdPlayer name = " + createdPlayer.getName());

            presentInfoView(createdPlayer);
        }
    }

    private void presentInfoView(PlayerInstance activePlayer) {
        //go back to Info view
        Bundle bundle = new Bundle();
        bundle.putSerializable("activePlayer", activePlayer);
        Intent intent = new Intent(this, WaitingActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        //overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.join);
        ToolbarConfig toolbarConfig = new ToolbarConfig(this, "Join Game");
        toolbarConfig.getToolbarBackButton().setText( "Back" );

        findViewById(R.id.join_game_button).setOnClickListener(this);
        findViewById(R.id.toolbar_back_button).setOnClickListener(this);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.join_game_button) {
            EditText gameName = (EditText)findViewById(R.id.j_game_text);
            EditText playerName = (EditText)findViewById(R.id.j_name_text);

            JoinRequestWrapper jrw = new JoinRequestWrapper();
            jrw.gameName = gameName.getText().toString();
            jrw.playerName = playerName.getText().toString();

            Log.i("JoinActivity", "joining a game");
            new JoinGameTask().execute(jrw);

            /*
            Bundle bundle = new Bundle();
            bundle.putSerializable("playerInstance", new PlayerInstance(name.getText().toString(), Double.valueOf(amount.getText().toString())));
            Intent intent = new Intent();
            intent.setClass(this, WaitingActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            */
        }
        else if (view.getId() == R.id.toolbar_back_button) {
            Intent intent = new Intent(this, GameView.class);
            startActivity(intent);
        }
    }
}
